package com.etang.a2020c1020.tools;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import java.util.Random;

public class CodePass {
    private static String code_all = "1234567890qwertytrupoiuasljkdfhjjfkgzmxcvm1234567980QIOWEYRWEOUITYQIWTPAHSDFKLZxvcn1324657980";
    public static String code = "";

    public static Bitmap createBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(160, 50, Bitmap.Config.ARGB_8888);
        StringBuffer stringBuffer = new StringBuffer();
        Canvas canvas = new Canvas(bitmap);
        Random random = new Random();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(30);
        for (int i = 0; i < 4; i++) {
            stringBuffer.append(code_all.charAt(random.nextInt(code_all.length())));
        }
        code = stringBuffer.toString();
        int base_top = 40;
        int base_left = 20;
        int random_left = 20;
        int random_top = 0;
        for (int i = 0; i < code.length(); i++) {
            paint.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            paint.setFakeBoldText(random.nextBoolean());
            random_top = base_top + random.nextInt(10);
            canvas.drawText(code.charAt(i) + "", base_left, random_top, paint);
            base_left += random_left;
        }
        canvas.drawLine(random.nextInt(canvas.getWidth()), random.nextInt(canvas.getHeight()), random.nextInt(canvas.getWidth()), random.nextInt(canvas.getHeight()), paint);
        canvas.save();
        canvas.restore();
        return bitmap;
    }
}
